/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package classes;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Alvaro
 */
public class comment {

    private int id;
    String name;
    String comment;
    String product;

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public comment(String name, String comment) {
        this.name = name;
        this.comment = comment;
    }

    public String getProduct() {
        return product;
    }

    public void setProduct(String product) {
        this.product = product;
    }

    public comment() {
    }

    /**
     * ************ DB *************************
     */
    public boolean guardar() throws SQLException {

        ObjetoDB o = new ObjetoDB();


        o.setTabla("comment");
        o.addCampo("name", name);
        o.addCampo("comment", comment);
        o.addCampo("product", product);


        AdminDB adb = new AdminDB();
        return adb.guardar(o);
    }

    public void getByName(Connection conn, String name) throws SQLException {
        ResultSet rset = null;
        try {
            PreparedStatement stmt = null;

            if (null == stmt) {
                stmt = conn.prepareStatement("SELECT * FROM `F1shopOnline`.`comment` WHERE name='" + name + "';");
                System.out.println("Listado de comentarios...");
            }
            rset = stmt.executeQuery();
            if (rset.next()) {

                this.setId(rset.getInt("id"));
                this.name = rset.getString("name");
                this.comment = rset.getString("comment");
                this.product = rset.getString("product");
            }
            if (stmt != null) {
                stmt.close();
            }
            

        } catch (SQLException e) {
            System.out.println("Error de MySQL: " + e.getMessage());
        } catch (Exception e) {
            Logger.getLogger("Registrar comentario").log(Level.SEVERE, "ERROR: " + e.getLocalizedMessage());
        }
        finally {
            if (rset != null) {
                try {
                    rset.close();
                } catch (SQLException e) {
                    Logger.getLogger("GetByName").log(Level.SEVERE, "ERROR AL CERRAR RSET: " + e.getLocalizedMessage());
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    Logger.getLogger("GetByName").log(Level.SEVERE, "ERROR AL CERRAR LA CONEXION: " + e.getLocalizedMessage());
                }
            }
        }
    }

    public boolean borrar(String id) throws SQLException {
        AdminDB adb = new AdminDB();
        int cpID = adb.getID("comment", "id='" + id + "'");
        ObjetoDB o = new ObjetoDB();
        o.setTabla("comment");
        o.addCampo("id", id);
        return adb.borrar(o);

    }

    public boolean borrarTodosComentarios() throws SQLException {
        AdminDB adb = new AdminDB();
        ObjetoDB o = new ObjetoDB();
        o.setTabla("comment");
        return adb.vaciarTabla(o);
    }

    public List<comment> getAllComentarios(Connection conn, String name) throws SQLException {
        ResultSet rset = null;
        try {
            PreparedStatement stmt = null;

            if (null == stmt) {
                stmt = conn.prepareStatement("SELECT * FROM `F1shopOnline`.`comment` WHERE product = '" + name + "'");
                System.out.println("Listado de comentarios...");
            }
            rset = stmt.executeQuery();
            List<comment> com = new ArrayList<comment>();
            while (rset.next()) {
                comment c = new comment();
                c.setId(rset.getInt("id"));
                c.setName(rset.getString("name"));
                c.setComment(rset.getString("comment"));
                c.setProduct(rset.getString("product"));
                com.add(c);
            }
            if (stmt != null) {
                stmt.close();
            }
            
            return com;
        } catch (SQLException e) {
            System.out.println("Error de MySQL: " + e.getMessage());
            return null;
        } catch (Exception e) {
            Logger.getLogger("Comentario").log(Level.SEVERE, "ERROR: " + e.getLocalizedMessage());
            return null;
        }
        finally {
            if (rset != null) {
                try {
                    rset.close();
                } catch (SQLException e) {
                    Logger.getLogger("GetAllComentarios").log(Level.SEVERE, "ERROR AL CERRAR RSET: " + e.getLocalizedMessage());
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    Logger.getLogger("GetAllComentarios").log(Level.SEVERE, "ERROR AL CERRAR LA CONEXION: " + e.getLocalizedMessage());
                }
            }
        }
    }
}
